package com.zhuchaooasystem.ui.home.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hwangjr.rxbus.RxBus;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.zhuchaooasystem.R;
import com.zhuchaooasystem.adapter.holder.ServiceViewHolder;
import com.zhuchaooasystem.base.BaseEvent;
import com.zhuchaooasystem.base.BaseFragment;
import com.zhuchaooasystem.common.Key;
import com.zhuchaooasystem.entity.OrderStateEntity;
import com.zhuchaooasystem.entity.ServiceEntity;
import com.zhuchaooasystem.presenter.home.fragment.ServiceContract;
import com.zhuchaooasystem.presenter.home.fragment.ServicePresenter;
import com.zhuchaooasystem.ui.home.service.ServiceSearchActivity;
import com.zhuchaooasystem.utils.DialogUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author yinsi
 */

public class ServiceFragment extends BaseFragment<ServicePresenter> implements ServiceContract.View,
        SwipeRefreshLayout.OnRefreshListener, RecyclerArrayAdapter.OnLoadMoreListener {


    @BindView(R.id.service_state)
    TextView serviceState;
    @BindView(R.id.service_filter)
    TextView serviceFilter;
    @BindView(R.id.service_search)
    TextView serviceSearch;
    @BindView(R.id.service_recyclerView)
    EasyRecyclerView recyclerView;
    @BindView(R.id.fragment_service_title)
    RelativeLayout fragmentServiceTitle;

    @BindView(R.id.fragmelayout_service_state)
    FrameLayout fragmelayoutServiceState;

    @BindView(R.id.fragmelayout_service_filter)
    FrameLayout fragmelayoutServiceFilter;
    @BindView(R.id.activity_home)
    RelativeLayout activityHome;

    @BindView(R.id.fragmelayout_service_state_blank_view)
    View serviceStateBlank;
    @BindView(R.id.fragmelayout_service_filter_blank_view)
    View ServiceFilterBlankView;
    //服务状态
    @BindView(R.id.layout_service_state)
    LinearLayout layoutServiceState;

    @BindView(R.id.fragmelayout_service_state_layout)
    LinearLayout fragmelayout_service_state_layout;

    @BindView(R.id.fragmelayout_service_filter_layout)
    RelativeLayout fragmelayout_service_filter_layout;
    @BindView(R.id.fragmelayout_service_filter_complete_tv)
    TextView fragmelayoutServiceFilterCompleteTv;

    //筛选抽屉
    @BindView(R.id.fragmelayout_service_filter_layout_subscribe_time)
    LinearLayout fragmelayoutServiceFilterLayoutSubscribeTime;
    @BindView(R.id.fragmelayout_service_filter_layout_subscribe_time_content)
    LinearLayout fragmelayoutServiceFilterLayoutSubscribeTimeContent;
    @BindView(R.id.fragmelayout_service_filter_layout_delay)
    LinearLayout fragmelayoutServiceFilterLayoutDelay;
    @BindView(R.id.fragmelayout_service_filter_layout_delay_content)
    LinearLayout fragmelayoutServiceFilterLayoutDelayContent;
    @BindView(R.id.filter_layout_subscribe_no_limit)
    TextView filterLayoutSubscribeNoLimit;
    @BindView(R.id.filter_layout_subscribe_today)
    TextView filterLayoutSubscribeToday;
    @BindView(R.id.filter_layout_subscribe_one_week)
    TextView filterLayoutSubscribeOneWeek;
    @BindView(R.id.filter_layout_delay_all)
    TextView filterLayoutDelayAll;
    @BindView(R.id.filter_layout_delay_yes)
    TextView filterLayoutDelayYes;
    @BindView(R.id.filter_layout_delay_no)
    TextView filterLayoutDelayNo;

    private List<String> subscribeList;
    private List<String> delayList;

    private RecyclerArrayAdapter adapter;

    private Dialog loadingDiaog;

    private int page;
    //用来标记recycleview加载的是那种类型的数据  例如正常的 服务状态 筛选
    //默认为正常的
//    private String flag = Key.SERVICE_NORMAL;

    private int[] imgRes = {R.drawable.ic_all_service, R.drawable.ic_new_service,
            R.drawable.ic_start_service, R.drawable.ic_end_service,};


    private String isOnWeek = "";//记录时间筛选

    private String orderState = "";//记录订单状态

    @Override
    protected ServicePresenter getPresenter() {
        return new ServicePresenter(mContext, this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_service;
    }

    @Override
    protected void initVariables() {
        page = 1;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    protected void initView() {


        subscribeList = new ArrayList<>();
        delayList = new ArrayList<>();

        loadingDiaog = DialogUtils.LoadingDialog(mContext);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setRefreshingColor(Key.RECYCLEVIEW_REFRESH_COLORS);
        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(getContext()) {
                    @Override
                    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {

                        return new ServiceViewHolder(parent);
                    }
                }
        );
        recyclerView.setEmptyView(R.layout.view_empty);
        adapter.setNoMore(R.layout.view_nomore, new RecyclerArrayAdapter.OnNoMoreListener() {
            @Override
            public void onNoMoreShow() {
                adapter.resumeMore();
            }

            @Override
            public void onNoMoreClick() {
                adapter.resumeMore();
            }
        });
        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });


        recyclerView.setRefreshListener(this);
        adapter.setMore(R.layout.view_more, this);


        //加载数据
//        flag = Key.SERVICE_NORMAL;
        //第一次进入时间为不限
        isOnWeek = "-1";
        orderState = "-1";
        mPresenter.downServiceStateData(page, isOnWeek, orderState);

        mPresenter.downOrderState();
    }


    @Override
    protected void initDataAndLoadData() {

    }

    private void initServiceState(final OrderStateEntity entity) {
        Collections.sort(entity.getStatus(), new Comparator<OrderStateEntity.StatusBean>() {
            @Override
            public int compare(OrderStateEntity.StatusBean statusBean1, OrderStateEntity.StatusBean statusBean2) {
                if (statusBean1.getItemValue() > statusBean2.getItemValue()) {
                    return 1;
                } else {
                    return -1;
                }
            }
        });
        for (int i = 0; i < imgRes.length; i++) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.view_layout_service_state, null);

            ImageView image = (ImageView) view.findViewById(R.id.view_layout_service_state_iv);

            image.setBackgroundResource(imgRes[i]);

            TextView tv = (TextView) view.findViewById(R.id.view_layout_service_state_tv);
            tv.setText(entity.getStatus().get(i).getItemName());

            layoutServiceState.addView(view);


            final int j = i;

            //服务状态几个选项的点击事件
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    serviceState.setTextColor(getResources().getColor(R.color.color_767676));

//                    flag = Key.SERVICE_STATE;
                    page = 1;
                    orderState = entity.getStatus().get(j).getItemValue() + "";
                    RxBus.get().post("refreshTitle", new BaseEvent<String>(entity.getStatus().get(j).getItemName()));
                    isOnWeek = "-1";
                    mPresenter.downServiceStateData(page, isOnWeek, orderState);

                    loadingDiaog.show();

                    //关闭右侧抽屉
                    fragmelayoutServiceState.startAnimation(AnimationUtils.loadAnimation(getActivity(),
                            R.anim.slide_out_right));
                    fragmelayoutServiceState.setVisibility(View.GONE);
                }
            });
        }
    }

    @OnClick({R.id.service_state, R.id.service_filter, R.id.service_search, R.id.fragmelayout_service_state_blank_view,
            R.id.fragmelayout_service_filter_blank_view, R.id.fragmelayout_service_state_layout,
            R.id.fragmelayout_service_filter_layout, R.id.fragmelayout_service_filter_complete_tv,
            R.id.fragmelayout_service_filter_layout_subscribe_time, R.id.fragmelayout_service_filter_layout_delay,
            R.id.filter_layout_subscribe_no_limit, R.id.filter_layout_subscribe_today,
            R.id.filter_layout_subscribe_one_week, R.id.filter_layout_delay_all, R.id.filter_layout_delay_yes,
            R.id.filter_layout_delay_no})
    public void onClick(View view) {
        switch (view.getId()) {
            //服务状态
            case R.id.service_state:

                serviceState.setTextColor(getResources().getColor(R.color.color_19c4ad));
                serviceFilter.setTextColor(getResources().getColor(R.color.color_767676));

                //如果筛选页面显示  先关闭  然后显示服务状态页面
                if (fragmelayoutServiceFilter.isShown()) {
                    fragmelayoutServiceFilter.startAnimation(AnimationUtils.loadAnimation(getActivity(),
                            R.anim.slide_out_right));
                    fragmelayoutServiceFilter.setVisibility(View.GONE);

                    fragmelayoutServiceState.startAnimation(AnimationUtils.loadAnimation(getActivity(),
                            R.anim.slide_in_right));
                    fragmelayoutServiceState.setVisibility(View.VISIBLE);
                    //如果服务状态显示  则隐藏
                } else if (fragmelayoutServiceState.isShown()) {

                    serviceState.setTextColor(getResources().getColor(R.color.color_767676));

                    fragmelayoutServiceState.startAnimation(AnimationUtils.loadAnimation(getActivity(),
                            R.anim.slide_out_right));
                    fragmelayoutServiceState.setVisibility(View.GONE);
                } else {//如果筛选页面没显示  直接显示服务状态页面
                    fragmelayoutServiceState.startAnimation(AnimationUtils.loadAnimation(getActivity(),
                            R.anim.slide_in_right));
                    fragmelayoutServiceState.setVisibility(View.VISIBLE);
                }

                break;
            //筛选
            case R.id.service_filter:
                serviceState.setTextColor(getResources().getColor(R.color.color_767676));
                serviceFilter.setTextColor(getResources().getColor(R.color.color_19c4ad));

                //如果服务状态页面显示  先关闭  然后显示筛选页面
                if (fragmelayoutServiceState.isShown()) {
                    fragmelayoutServiceState.startAnimation(AnimationUtils.loadAnimation(getActivity(),
                            R.anim.slide_out_right));
                    fragmelayoutServiceState.setVisibility(View.GONE);
                    isOnWeek = "-1";
                    filterLayoutSubscribeNoLimit.setTextColor(getContext().getResources().getColor(R.color.color_19c4ad));
                    filterLayoutSubscribeToday.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));
                    filterLayoutSubscribeOneWeek.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));
                    filterLayoutDelayAll.setTextColor(getContext().getResources().getColor(R.color.color_19c4ad));
                    filterLayoutDelayYes.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));
                    filterLayoutDelayNo.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));

                    fragmelayoutServiceFilter.startAnimation(AnimationUtils.loadAnimation(getActivity(),
                            R.anim.slide_in_right));
                    fragmelayoutServiceFilter.setVisibility(View.VISIBLE);
                    //如果筛选显示则隐藏
                } else if (fragmelayoutServiceFilter.isShown()) {

                    serviceFilter.setTextColor(getResources().getColor(R.color.color_767676));

                    fragmelayoutServiceFilter.startAnimation(AnimationUtils.loadAnimation(getActivity(),
                            R.anim.slide_out_right));
                    fragmelayoutServiceFilter.setVisibility(View.GONE);
                } else {//如果服务状态页面没显示  直接显示筛选页面
                    isOnWeek = "-1";
                    filterLayoutSubscribeNoLimit.setTextColor(getContext().getResources().getColor(R.color.color_19c4ad));
                    filterLayoutSubscribeToday.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));
                    filterLayoutSubscribeOneWeek.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));
                    filterLayoutDelayAll.setTextColor(getContext().getResources().getColor(R.color.color_19c4ad));
                    filterLayoutDelayYes.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));
                    filterLayoutDelayNo.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));

                    fragmelayoutServiceFilter.startAnimation(AnimationUtils.loadAnimation(getActivity(),
                            R.anim.slide_in_right));
                    fragmelayoutServiceFilter.setVisibility(View.VISIBLE);
                }
                break;
            //搜索
            case R.id.service_search:

                Intent intent = new Intent(getContext(), ServiceSearchActivity.class);
                startActivity(intent);

                break;
            //点击空白区域  隐藏服务状态
            case R.id.fragmelayout_service_state_blank_view:

                if (fragmelayoutServiceState.isShown()) {

                    serviceState.setTextColor(getResources().getColor(R.color.color_767676));

                    fragmelayoutServiceState.startAnimation(AnimationUtils.loadAnimation(getActivity(),
                            R.anim.slide_out_right));
                    fragmelayoutServiceState.setVisibility(View.GONE);
                }
                break;
            //点击抽屉空白区域 会点击到recycleView
            case R.id.fragmelayout_service_state_layout:
                break;

            //点击空白区域  隐藏筛选
            case R.id.fragmelayout_service_filter_blank_view:
                if (fragmelayoutServiceFilter.isShown()) {

                    serviceFilter.setTextColor(getResources().getColor(R.color.color_767676));

                    fragmelayoutServiceFilter.startAnimation(AnimationUtils.loadAnimation(getActivity(),
                            R.anim.slide_out_right));
                    fragmelayoutServiceFilter.setVisibility(View.GONE);
                }
                break;
            //点击抽屉空白区域 会点击到recycleView
            case R.id.fragmelayout_service_filter_layout:
                break;
            //帅选抽屉的完成
            case R.id.fragmelayout_service_filter_complete_tv:
//                RxBus.get().post("refreshTitle", new BaseEvent<String>("全部服务"));
                page = 1;
//                flag = Key.SERVICE_NORMAL;
                mPresenter.downServiceStateData(page, isOnWeek, orderState);

                loadingDiaog.show();

                if (fragmelayoutServiceFilter.isShown()) {

                    serviceFilter.setTextColor(getResources().getColor(R.color.color_767676));

                    fragmelayoutServiceFilter.startAnimation(AnimationUtils.loadAnimation(getActivity(),
                            R.anim.slide_out_right));
                    fragmelayoutServiceFilter.setVisibility(View.GONE);
                }

                break;
            //筛选抽屉---预约时间
            case R.id.fragmelayout_service_filter_layout_subscribe_time:
                if (fragmelayoutServiceFilterLayoutSubscribeTimeContent.isShown()) {
                    fragmelayoutServiceFilterLayoutSubscribeTimeContent.setVisibility(View.GONE);
                } else {
                    fragmelayoutServiceFilterLayoutSubscribeTimeContent.setVisibility(View.VISIBLE);
                }
                break;
            //筛选抽屉--是否延迟
            case R.id.fragmelayout_service_filter_layout_delay:

                if (fragmelayoutServiceFilterLayoutDelayContent.isShown()) {
                    fragmelayoutServiceFilterLayoutDelayContent.setVisibility(View.GONE);
                } else {
                    fragmelayoutServiceFilterLayoutDelayContent.setVisibility(View.VISIBLE);
                }

                break;
            //筛选抽屉--不限
            case R.id.filter_layout_subscribe_no_limit:
                isOnWeek = "-1";

                filterLayoutSubscribeNoLimit.setTextColor(getContext().getResources().getColor(R.color.color_19c4ad));
                filterLayoutSubscribeToday.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));
                filterLayoutSubscribeOneWeek.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));
                break;
            //筛选抽屉--今天
            case R.id.filter_layout_subscribe_today:
                isOnWeek = "0";

                filterLayoutSubscribeNoLimit.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));
                filterLayoutSubscribeToday.setTextColor(getContext().getResources().getColor(R.color.color_19c4ad));
                filterLayoutSubscribeOneWeek.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));

                break;
            //筛选抽屉--一周内
            case R.id.filter_layout_subscribe_one_week:
                isOnWeek = "1";

                filterLayoutSubscribeNoLimit.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));
                filterLayoutSubscribeToday.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));
                filterLayoutSubscribeOneWeek.setTextColor(getContext().getResources().getColor(R.color.color_19c4ad));

                break;
            //筛选抽屉--全部
            case R.id.filter_layout_delay_all:
                filterLayoutDelayAll.setTextColor(getContext().getResources().getColor(R.color.color_19c4ad));
                filterLayoutDelayYes.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));
                filterLayoutDelayNo.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));
                break;
            //筛选抽屉--是
            case R.id.filter_layout_delay_yes:
                filterLayoutDelayAll.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));
                filterLayoutDelayYes.setTextColor(getContext().getResources().getColor(R.color.color_19c4ad));
                filterLayoutDelayNo.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));
                break;
            //筛选抽屉--否
            case R.id.filter_layout_delay_no:
                filterLayoutDelayAll.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));
                filterLayoutDelayYes.setTextColor(getContext().getResources().getColor(R.color.color_7f7f7f));
                filterLayoutDelayNo.setTextColor(getContext().getResources().getColor(R.color.color_19c4ad));
                break;
        }
    }


    /**
     * 加载第一页和 下拉刷新的数据  需要先清空adapter里面的数据
     *
     * @param entity
     */
    @Override
    public void showData(ServiceEntity entity) {

        loadingDiaog.dismiss();

        adapter.clear();
        adapter.addAll(entity.getResult().getSuccessful().getResponseFlow());
    }

    /**
     * 上拉刷新加载更多数据
     *
     * @param entity
     */
    @Override
    public void showMoreData(ServiceEntity entity) {

        loadingDiaog.dismiss();

        adapter.addAll(entity.getResult().getSuccessful().getResponseFlow());
    }

    @Override
    public void showOrderState(OrderStateEntity entity) {

        loadingDiaog.dismiss();
        //右侧服务状态隐藏布局添加内容
        initServiceState(entity);
    }

    @Override
    public void onRefresh() {
        page = 1;
        //正常情况下的刷新
//        if (flag.equals(Key.SERVICE_NORMAL)) {
//            mPresenter.downServiceStateData(page, isOnWeek, "-1");
//        }
//        //带服务状态的
//        else if (flag.equals(Key.SERVICE_STATE)) {
//        isOnWeek = "-1";
        mPresenter.downServiceStateData(page, isOnWeek, orderState);
//        }
    }

    @Override
    public void onLoadMore() {
        page++;
        //正常情况下的刷新
//        if (flag.equals(Key.SERVICE_NORMAL)) {
//            mPresenter.downServiceStateData(page, isOnWeek, "-1");
//        }
//        带服务状态的
//        else if (flag.equals(Key.SERVICE_STATE)) {
//        isOnWeek = "-1";
        mPresenter.downServiceStateData(page, isOnWeek, orderState);
//        }
    }

    public void RedactToService() {
//        flag = Key.SERVICE_NORMAL;
        page = 1;
        isOnWeek = "-1";

        mPresenter.downServiceStateData(page, isOnWeek, orderState);

        loadingDiaog.show();
    }

    public void changeState(int state) {

//        flag = Key.SERVICE_STATE;
        page = 1;
        orderState = state + "";
        isOnWeek = "-1";
        mPresenter.downServiceStateData(page, isOnWeek, orderState);

        loadingDiaog.show();
    }


}
